﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TireBias.Models;
using JQueryDataTables.Models;
using System.Data;

namespace TireBias.Controllers
{
    public class BIASWASTEController : Controller
    {
        //
        // GET: /BIASWASTE/

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult AddWaste(string biasno,string returnUrl="",string tirecode="",string toplot="",string toproll="")
        {
            try
            {
            ViewBag.Full = "F";
            ViewBag.Return = returnUrl.Replace("|","&");

            TireBias.Models.SessionBag.Current.biasno = biasno;

            TailModel getShift = new TailModel();
            DataTable dt = getShift.GET_SHIFT();
            TireBias.Models.SessionBag.Current.shift = dt.Rows[0]["shift"].ToString();
            TireBias.Models.SessionBag.Current.curdate = dt.Rows[0]["CurDate"];
            TireBias.Models.SessionBag.Current.tirecode = tirecode;
            TireBias.Models.SessionBag.Current.toplot = toplot;
            TireBias.Models.SessionBag.Current.toproll = toproll;
            return View();
            }
            catch (Exception ex)
            {
                WinNemoLib.Nemo.Error(ex);
                throw;
            }
       
        }

        public JsonResult List_Wast(JQueryDataTableParamModel param)
        {
            WasteModel m = new WasteModel();       
            IEnumerable<WasteModel.RES_WASTE_DS> IElist;
            IElist = m.SEL_WASTE(WinNemoLib.DateUtil.ToDisplayDate(TireBias.Models.SessionBag.Current.curdate
                , WinNemoLib.DateUtil.eLanguage.EN), param, SessionBag.Current.shift, SessionBag.Current.biasno
                , TireBias.Models.SessionBag.Current.tirecode 
                , TireBias.Models.SessionBag.Current.toplot, TireBias.Models.SessionBag.Current.toproll);
            var display = IElist.Skip(param.iDisplayStart).Take(param.iDisplayLength);
            var result = from c in display select new[] { c._Reason,c._Qty };
            return Json(new
            {
                sEcho = param.sEcho,
                iTotalRecords = IElist.Count(),
                iTotalDisplayRecords = IElist.Count(),
                aaData = result
            },
                         JsonRequestBehavior.AllowGet);
        }

        public JsonResult Get_Detail()
        {
            WasteModel rs = new WasteModel();
            List<WasteModel.Waste_DS> list = rs.SEL_WASTE_MASTER("");
            return Json(list);
        }

        public JsonResult Get_TireCord(string topcode,string reasoncode)
        {
            TopCordBomModel tire = new TopCordBomModel();
            string[] arr;
            arr=tire.LIST_TOPCORDBOM(topcode,"","","tirecode");
            if (arr == null)
            {
                return Json(new { success = 0, message = "ไม่พบข้อมูล Topped Code ที่ระบุ" });
            }
            WasteModel rs = new WasteModel();
            List<WasteModel.Waste_DS> list = rs.SEL_WASTE_MASTER(reasoncode);
            if (list.Count<=0)
            {
                return Json(new { success = 0, message = "ไม่พบข้อมูล Reason ที่ระบุ" });
            }
            return Json(new { success = 1, message = "" });
        }

        public JsonResult Add_RSWaste(string topcode)
        {
            TopCordBomModel tire = new TopCordBomModel();
            string[] arr;
            arr=tire.LIST_TOPCORDBOM(topcode,"","","tirecode");
            if (arr == null)
            {
                return Json(new { success = 0, message = "ไม่พบข้อมูลที่ระบุ" });
            }
            return Json(new { success = 1, message = "" });
        }



        public JsonResult INS_WASTE_DATA(TireBias.Models.WasteModel.RES_WASTE_DS theData)
        {
            try
            {

            TireBias.Models.WasteModel mdUpd = new TireBias.Models.WasteModel();

            if (mdUpd.Ins_Data(theData))
            {
                return Json(new { success = 1, message = "บันทึกเรียบร้อย" });
            }
            else
            {
                return Json(new { success = 0, message = "ไม่สามารถบันทึกได้" });
            }
            }
            catch (Exception ex)
            {
                WinNemoLib.Nemo.Error(ex);
                throw;
            }



        }

    }
}
